package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.ProductDetailActivity;
import com.wwp.tbshop.model.bean.GoodsListBean;
import com.wwp.tbshop.util.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 楠GG on 2017/6/17.
 */

public class GoodsListLvAdapter extends BaseAdapter {
    private static final String TAG = "GoodsListLvAdapter";

    private Context mContext;
    private List<GoodsListBean.ProductListBean> mDatas = new ArrayList<>();

    public GoodsListLvAdapter(Context context) {
        mContext = context;
    }

    public void setDatas(List<GoodsListBean.ProductListBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NormalHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_goods_list, parent, false);
            holder = new NormalHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (NormalHolder) convertView.getTag();
        }
        holder.setData(mDatas.get(position));
        return convertView;
    }

    class NormalHolder {
        @Bind(R.id.iv_goods)
        ImageView mIvGoods;
        @Bind(R.id.tv_name)
        TextView mTvName;
        @Bind(R.id.tv_newprice)
        TextView mTvNewprice;
        @Bind(R.id.tv_oldprice)
        TextView mTvOldprice;
        @Bind(R.id.tv_rmb)
        TextView mTvRmb;
        @Bind(R.id.tv_comment)
        TextView mTvCommentNum;
        @Bind(R.id.ll_price)
        LinearLayout mLlPrice;
        private GoodsListBean.ProductListBean mProductListBean;

        NormalHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pId = mProductListBean.id;
                    Intent intent = new Intent(mContext, ProductDetailActivity.class);
                    intent.putExtra("pId", pId);
                    mContext.startActivity(intent);
                }
            });
        }

        public void setData(GoodsListBean.ProductListBean bean) {
            mProductListBean = bean;
            mTvName.setText(bean.name);
            mTvCommentNum.setText(bean.commentCount + "人评论");
            mTvNewprice.setText(bean.price + "");
            if (bean.price == bean.marketPrice) {
                mTvOldprice.setVisibility(View.GONE);
                mTvRmb.setVisibility(View.GONE);
            } else {
                mTvOldprice.setText(bean.marketPrice + "");
                mTvOldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }
            Picasso.with(mContext).load(Constant.HOST + bean.pic).into(mIvGoods);
        }
    }


}
